<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico">
  <link rel="mask-icon" href="/images/favicon.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="笔记，常见问题">
<meta property="og:type" content="article">
<meta property="og:title" content="ReactNative学习">
<meta property="og:url" content="http://yoursite.com/2019/01/01/android-2019-1-1-ReactNative/index.html">
<meta property="og:site_name" content="Akiyama&#39;s Blog">
<meta property="og:description" content="笔记，常见问题">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yoursite.com/images/ReactNative/vscode_format.png">
<meta property="article:published_time" content="2018-12-31T16:00:00.000Z">
<meta property="article:modified_time" content="2021-05-16T11:33:55.046Z">
<meta property="article:author" content="丘山月夜">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="ReactNative">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yoursite.com/images/ReactNative/vscode_format.png">

<link rel="canonical" href="http://yoursite.com/2019/01/01/android-2019-1-1-ReactNative/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>ReactNative学习 | Akiyama's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Akiyama's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">丘山月夜的博客</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="home fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/01/01/android-2019-1-1-ReactNative/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="丘山月夜">
      <meta itemprop="description" content="君は空を見てるか,<br>風の音を聞いてるか">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Akiyama's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ReactNative学习
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-01-01 00:00:00" itemprop="dateCreated datePublished" datetime="2019-01-01T00:00:00+08:00">2019-01-01</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-05-16 19:33:55" itemprop="dateModified" datetime="2021-05-16T19:33:55+08:00">2021-05-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/" itemprop="url" rel="index"><span itemprop="name"></span></a>
                </span>
            </span>

          
            <div class="post-description">笔记，常见问题</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="1-和传统前端的区别【转】"><a href="#1-和传统前端的区别【转】" class="headerlink" title="1. 和传统前端的区别【转】"></a>1. 和传统前端的区别<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/91dd6ae96a26">【转】</a></h1><h2 id="1-1-标签不同"><a href="#1-1-标签不同" class="headerlink" title="1.1. 标签不同"></a>1.1. 标签不同</h2><ul>
<li>容器：div——View</li>
<li>图片    img——Image</li>
<li>文字    所有双标签——Text</li>
<li>背景    background——ImageBackground</li>
</ul>
<p><strong>注意:</strong></p>
<ol>
<li><p>react native中所有的标签首字母必须大写，而html标签标准都是小写</p>
</li>
<li><p>react native中所有需要输出的文字必须使用<code>&lt;Text&gt;&lt;/Text&gt;</code>包裹,而HTML里面的双标签都可以包裹文字</p>
</li>
<li><p>react native的style中不支持使用background来定义背景，如果想要用图片做背景，必须使用<code>&lt;ImageBackground source=&#123;require(&quot;bg.png&quot;)&#125;&gt;&lt;/ImageBackground&gt;</code>标签包裹，而html中的css中可以使用：<code>background:url(&quot;../images/bg.png&quot;)</code>。</p>
</li>
<li><p>HTML中的图片：<code>&lt;img src=&quot;a.jpg&quot;/&gt;</code>，react native中的图片必须指定宽高：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;Image source&#x3D;&quot;&#123;&#123; uri: thumb &#125;&#125;&quot; style&#x3D;&quot;&#123;&#123; width:100, height:100 &#125;&#125;&quot; &#x2F;&gt;</span><br></pre></td></tr></table></figure></li>
</ol>
<h2 id="1-2-布局样式不同"><a href="#1-2-布局样式不同" class="headerlink" title="1.2. 布局样式不同"></a>1.2. 布局样式不同</h2><p>react native中不存在float，也不支持position:fixed，而是采用的flex布局，虽然react native支持position中的absolute和realative以及flex,都是和html中css有所不同:</p>
<ul>
<li>react native中的absolute默认相对于父级，就算父级没有定位，而css中的absolute是向上寻找的相对点，如果父级没有定位，一直往上找定位参考点</li>
<li>react native中的flex的flexDirection默认值是:column而css中的默认值是row</li>
<li>react native中的高度等于父级高度使用flex:1 ，而web的css中使用height:100%，react native中没有百分比的概念</li>
</ul>
<p><em>react native支持的属性：参考<a target="_blank" rel="noopener" href="https://reactnative.cn/docs/0.49/layout-props.html#content">属性</a></em></p>
<h2 id="1-3-结构不同"><a href="#1-3-结构不同" class="headerlink" title="1.3. 结构不同"></a>1.3. 结构不同</h2><p>html加载的时候是自上而下的阻塞式加载，而react native根据生命周期来渲染，跟写的位置无关，而且react native文件一切皆js，无论是结构还是css还是JS逻辑，下面是react native的生命周期:</p>
<ul>
<li>constructor:组件被创建之前初始化数据</li>
<li>componentWillMount组件已创建但是未被渲染，可以在这里面请求数据</li>
<li>render:组件渲染，组件结构都写在这里</li>
<li>componentDidMount:组件已渲染完,可以在这里请求数据并使用setState改变数据来触发视图自动更新</li>
<li>componentWillReceiveProps:如果组件收到新的属性（props），就会调用此函数,并使用setState改变数据来触发视图自动更新</li>
<li>shouldComponentUpdate当组件接收到新的属性和状态改变的话，都会触发调用 shouldComponentUpdate来判断组件是否应该更新</li>
<li>componentWillUpdate如果组件状态或者属性改变，并且上的 shouldComponentUpdate(…) 返回为 true，就会开始准更新组件，并调用 componentWillUpdate()</li>
<li>componentDidUpdate调用了 render() 更新完成界面之后，会调用 componentDidUpdate() 来得到通知</li>
<li>componentWillUnmount当组件要被从界面上移除的时候，就会调用此函数，一般在这里取消定时器，remove监听事件</li>
</ul>
<h2 id="1-4-页面跳转不同"><a href="#1-4-页面跳转不同" class="headerlink" title="1.4. 页面跳转不同"></a>1.4. 页面跳转不同</h2><p>html中使用a标签或者window.location.href来跳转页面，react native的路由跳转有多种方案,可以根据自身选择，官方推荐react-navigation，还有react-native-router-flux,navigator等</p>
<h2 id="1-5-js运行环境不同"><a href="#1-5-js运行环境不同" class="headerlink" title="1.5. js运行环境不同"></a>1.5. js运行环境不同</h2><p>web中的js运行在浏览器，最上级是window，而react native里没有window的概念,组件化最上级是global,挂在global下面是跨组件的，例如global.a=1，那么在所有组件都能使用a，所以一般登录成功后把token挂在global上面，在其他任何页面请求数据的时候可以直接使用token，注销登录就清除token</p>
<h1 id="2-常见问题"><a href="#2-常见问题" class="headerlink" title="2. 常见问题"></a>2. 常见问题</h1><h2 id="2-1-React-Native-Error-Cannot-find-module-‘asap-raw’"><a href="#2-1-React-Native-Error-Cannot-find-module-‘asap-raw’" class="headerlink" title="2.1. React Native:Error: Cannot find module ‘asap/raw’"></a>2.1. React Native:Error: Cannot find module ‘asap/raw’</h2><p>安装<code>react-native-navigation</code>后出错</p>
<p>解决办法：重新安装依赖，执行<code>npm install</code></p>
<h2 id="2-2-setting-gradle路径问题"><a href="#2-2-setting-gradle路径问题" class="headerlink" title="2.2. setting.gradle路径问题"></a>2.2. setting.gradle路径问题</h2><p>使用react-navigation：</p>
<ul>
<li>yarn add react-navigation</li>
<li>yarn add react-native-gesture-handler</li>
<li>react-native link react-native-gesture-handler （会修改setting.gradle，引入react-native-gesture-handler作为project module）</li>
</ul>
<p>问题：路径使用<code>\</code>分割，不正确，应该修改为<code>/</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Settings file &#x27;E:\AndroidStudioProjects\Demo\android\settings.gradle&#x27; line: 3</span><br><span class="line"></span><br><span class="line">* What went wrong:</span><br><span class="line">Could not compile settings file &#x27;E:\AndroidStudioProjects\Demo\android\settings.gradle&#x27;.</span><br><span class="line"><span class="meta">&gt;</span><span class="bash"> startup failed:</span></span><br><span class="line">  settings file &#x27;E:\AndroidStudioProjects\Demo\android\settings.gradle&#x27;: 3: unexpected char: &#x27;\&#x27; @ line 3, column 133.</span><br><span class="line">     s\react-native-gesture-handler\android&#x27;)</span><br></pre></td></tr></table></figure>

<h2 id="2-3-VsCode格式化代码"><a href="#2-3-VsCode格式化代码" class="headerlink" title="2.3. VsCode格式化代码"></a>2.3. VsCode格式化代码</h2><p>VsCode格式化ReactNative代码的时候，标签尖括号会换行</p>
<p>解决办法：如果是js文件，下面不要选择JavaScript，而是JavaScript React，如图</p>
<p><img src="/images/ReactNative/vscode_format.png" alt="VsCode格式化ReactNative代码"></p>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/images/wechat_channel.jpg">
            <span class="icon">
              <i class="fab fa-weixin"></i>
            </span>

            <span class="label">WeChat</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/" rel="tag"><i class="fa fa-tag"></i> </a>
              <a href="/" rel="tag"><i class="fa fa-tag"></i> </a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2018/12/28/basic-2018-12-28-Network/" rel="prev" title="网络相关">
      <i class="fa fa-chevron-left"></i> 网络相关
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/01/01/algorithm-2019-01-01-SortAlgorithm/" rel="next" title="排序算法">
      排序算法 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="header-overlay"></div>
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E5%92%8C%E4%BC%A0%E7%BB%9F%E5%89%8D%E7%AB%AF%E7%9A%84%E5%8C%BA%E5%88%AB%E3%80%90%E8%BD%AC%E3%80%91"><span class="nav-number">1.</span> <span class="nav-text">1. 和传统前端的区别【转】</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-%E6%A0%87%E7%AD%BE%E4%B8%8D%E5%90%8C"><span class="nav-number">1.1.</span> <span class="nav-text">1.1. 标签不同</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E5%B8%83%E5%B1%80%E6%A0%B7%E5%BC%8F%E4%B8%8D%E5%90%8C"><span class="nav-number">1.2.</span> <span class="nav-text">1.2. 布局样式不同</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E7%BB%93%E6%9E%84%E4%B8%8D%E5%90%8C"><span class="nav-number">1.3.</span> <span class="nav-text">1.3. 结构不同</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC%E4%B8%8D%E5%90%8C"><span class="nav-number">1.4.</span> <span class="nav-text">1.4. 页面跳转不同</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-5-js%E8%BF%90%E8%A1%8C%E7%8E%AF%E5%A2%83%E4%B8%8D%E5%90%8C"><span class="nav-number">1.5.</span> <span class="nav-text">1.5. js运行环境不同</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98"><span class="nav-number">2.</span> <span class="nav-text">2. 常见问题</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-React-Native-Error-Cannot-find-module-%E2%80%98asap-raw%E2%80%99"><span class="nav-number">2.1.</span> <span class="nav-text">2.1. React Native:Error: Cannot find module ‘asap&#x2F;raw’</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-setting-gradle%E8%B7%AF%E5%BE%84%E9%97%AE%E9%A2%98"><span class="nav-number">2.2.</span> <span class="nav-text">2.2. setting.gradle路径问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-VsCode%E6%A0%BC%E5%BC%8F%E5%8C%96%E4%BB%A3%E7%A0%81"><span class="nav-number">2.3.</span> <span class="nav-text">2.3. VsCode格式化代码</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="丘山月夜"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">丘山月夜</p>
  <div class="site-description" itemprop="description">君は空を見てるか,<br>風の音を聞いてるか</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">60</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">丘山月夜</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
